package ldkxingzhe.l365calendar.widget.dialog.picker;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.utils.OSUtil;
import ldkxingzhe.l365calendar.widget.dialog.DialogFragmentBase;
import ldkxingzhe.l365calendar.widget.wheelview.OnWheelChangedListener;
import ldkxingzhe.l365calendar.widget.wheelview.WheelView;
import ldkxingzhe.l365calendar.widget.wheelview.adapter.NumericWheelAdapter;

/**
 * Created by ke on 16-1-31.
 */
public class TimePicker extends DialogFragmentBase implements OnWheelChangedListener {
    @SuppressWarnings("unused")
    private static final String TAG = "TimePicker";

    public static final String HOUR = "hour";
    public static final String MINUTE = "minute";

    private WheelView hour, minute;
    private int originHour, originMinute;
    private TextView title;

    public static TimePicker newInstance(int hour, int minute) {
        if(hour < 0 || hour > 23){
            Log.e(TAG, "hour is wrong, hour = " + hour);
            hour = 0;
        }
        if(minute < 0 || minute > 59){
            Log.e(TAG, "minute is wrong , minute is " + minute);
            minute = 0;
        }
        Bundle args = new Bundle();
        args.putInt(HOUR, hour);
        args.putInt(MINUTE, minute);
        TimePicker fragment = new TimePicker();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        originHour = bundle.getInt(HOUR, 0);
        originMinute = bundle.getInt(MINUTE, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.picker_real_time, container, false);
        hour = (WheelView) resultView.findViewById(R.id.hour);
        minute = (WheelView) resultView.findViewById(R.id.minute);
        title = (TextView) resultView.findViewById(R.id.title);
        title.setText(originHour + ":" + originMinute);
        initAdapter();
        initButtonLayout(resultView.findViewById(R.id.button_layout));
        return resultView;
    }

    private void initButtonLayout(View buttonLayout) {
        buttonLayout.findViewById(R.id.negative_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAndDealWithResult(Activity.RESULT_CANCELED, null);
            }
        });
        buttonLayout.findViewById(R.id.positive_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(HOUR, hour.getCurrentItem());
                intent.putExtra(MINUTE, minute.getCurrentItem());
                dismissAndDealWithResult(Activity.RESULT_OK, intent);
            }
        });
    }

    private void initAdapter() {
        NumericWheelAdapter hourAdapter = new NumericWheelAdapter(getActivity(), 0, 23);
        NumericWheelAdapter minuteAdapter = new NumericWheelAdapter(getActivity(), 0, 59);
        initTextSizeAndColor(hourAdapter);
        initTextSizeAndColor(minuteAdapter);
        hour.setViewAdapter(hourAdapter);
        minute.setViewAdapter(minuteAdapter);
        hour.setCurrentItem(originHour);
        minute.setCurrentItem(originMinute);
        hour.addChangingListener(this);
        minute.addChangingListener(this);
        hour.setCyclic(true);
        minute.setCyclic(true);
    }

    private void initTextSizeAndColor(NumericWheelAdapter adapter) {
        adapter.setTextSize((int) OSUtil.dp2px(10));
        adapter.setLabel("");
        adapter.setTextColor(Color.BLACK);
    }


    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        title.setText(hour.getCurrentItem() + ":" + minute.getCurrentItem());
    }
}
